package com.jindy.module.jpush.service.impl;

import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.utils.DateUtils;
import com.jindy.common.utils.StringUtils;
import com.jindy.jpush.utils.JPushUtils;
import com.jindy.module.jpush.domain.Jpush;
import com.jindy.module.jpush.mapper.JpushMapper;
import com.jindy.module.jpush.service.IJpushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * jpush消息推送Service业务层处理
 * 
 * @author jindy
 * @date 2023-04-02
 */
@Service
public class JpushServiceImpl implements IJpushService 
{
    private static final Logger LOGGER = LoggerFactory.getLogger(JpushServiceImpl.class);
    @Resource
    private JpushMapper jpushMapper;

    @Resource
    private JPushUtils jPushUtils;

    /**
     * 查询jpush消息推送
     * 
     * @param id jpush消息推送主键
     * @return jpush消息推送
     */
    @Override
    public Jpush selectJpushById(Long id)
    {
        return jpushMapper.selectJpushById(id);
    }

    /**
     * 查询jpush消息推送列表
     * 
     * @param jpush jpush消息推送
     * @return jpush消息推送
     */
    @Override
    public List<Jpush> selectJpushList(Jpush jpush)
    {
        return jpushMapper.selectJpushList(jpush);
    }

    /**
     * 新增jpush消息推送
     * 
     * @param jpush jpush消息推送
     * @return 结果
     */
    @Override
    @Transactional (rollbackFor = Exception.class)
    public int insertJpush(Jpush jpush)
    {
        LOGGER.info("获取的regid："+jpush.getRegistrationId());
        if (StringUtils.isBlank(jpush.getRegistrationId())){
            throw new RuntimeException("regId为空");
        }
        //判断用户regid是否产改变
        Jpush oldJpush = jpushMapper.selectJpushByUserId(jpush.getUserId());
        if (oldJpush==null){
            //用户regid为空直接新增
            jpush.setCreateTime(DateUtils.getNowDate());
            jpush.setState("1");
            jpush.setCreateBy(jpush.getUsername());
            jpush.setUpdateTime(DateUtils.getNowDate());
            jpush.setUpdateBy(jpush.getUsername());
            // AjaxResult result = jPushUtils.bindAliasAndDevice(jpush.getUserId().toString(), jpush.getRegistrationId());
            // LOGGER.info("bindAliasAndDevice:"+result);
            return jpushMapper.insertJpush(jpush);
        }else{
            //用户regid不为空,判断设备是否更改
            if(jpush.getRegistrationId().equals(oldJpush.getRegistrationId())){
                return 1;
            }
            jpush.setUpdateTime(DateUtils.getNowDate());
            jpush.setUpdateBy(jpush.getUsername());
            jpushMapper.updateJpushByUserId(jpush);
            //删除极光别名和设备regid
            // AjaxResult deleteResult = jPushUtils.deleteAliasDevice(jpush.getUserId().toString());
            // LOGGER.info("删除极光别名和设备:"+deleteResult);
            // AjaxResult bindResult = jPushUtils.bindAliasAndDevice(jpush.getUserId().toString(), jpush.getRegistrationId());
            // LOGGER.info("绑定极光别名和设备:"+bindResult);
            return 1;
        }
    }

    /**
     * 修改jpush消息推送
     * 
     * @param jpush jpush消息推送
     * @return 结果
     */
    @Override
    public int updateJpush(Jpush jpush)
    {
        jpush.setUpdateTime(DateUtils.getNowDate());
        return jpushMapper.updateJpush(jpush);
    }

    /**
     * 批量删除jpush消息推送
     * 
     * @param ids 需要删除的jpush消息推送主键
     * @return 结果
     */
    @Override
    public int deleteJpushByIds(Long[] ids)
    {
        return jpushMapper.deleteJpushByIds(ids);
    }

    /**
     * 删除jpush消息推送信息
     * 
     * @param id jpush消息推送主键
     * @return 结果
     */
    @Override
    public int deleteJpushById(Long id)
    {
        return jpushMapper.deleteJpushById(id);
    }
}
